package com.mustafaiev.tair.mweather.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.mustafaiev.tair.mweather.forecast.Forecast;
import com.mustafaiev.tair.mweather.forecast.Town;

/**
 * XML parser handler for forecast. Creation date: 28 Aug 2011 18:26:55
 * 
 * @author Tair Mustafaiev
 * 
 */
public class ForecastXMLHandler extends DefaultHandler {

	private Forecast forecast;
	private Town town;
	private ArrayList<Forecast> forecastList;

	public ArrayList<Forecast> getForecastList() {
		return this.forecastList;
	}

	public Forecast getForecast() {
		return this.forecast;
	}

	public Town getTown() {
		return this.town;
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		this.forecastList = new ArrayList<Forecast>();
		this.town = new Town();
		this.forecast = new Forecast();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);

		// set town
		this.setTown(qName, attributes);
		// set forecast
		this.setForecast(qName, attributes);

	}

	/**
	 * Method sets values for Town entity. Creation date: 28 Aug 2011 23:13:42
	 * 
	 * @param qName
	 * @param attributes
	 */
	private void setTown(String qName, Attributes attributes) {
		if (ForecastXMLElement.TOWN.toString().equalsIgnoreCase(qName)) {
			// set town index
			this.town
					.setTownId(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.INDEX.toString()
									.toLowerCase())));

			// set town name
			this.town.setTown(attributes.getValue(ForecastXMLElement.SNAME
					.toString().toLowerCase()));
			// set town latitude
			this.town.setLatitude(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.LATITUDE.toString()
							.toLowerCase())));
			// set town longitude
			this.town.setLongitude(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.LONGITUDE.toString()
							.toLowerCase())));
		}

		this.forecast.setTown(this.town);
	}

	/**
	 * Method sets the forecast entity. Creation date: 28 Aug 2011 23:41:53
	 * 
	 * @param qName
	 * @param attributes
	 */
	private void setForecast(String qName, Attributes attributes) {

		if (ForecastXMLElement.FORECAST.toString().equalsIgnoreCase(qName)) {
			// set day for forecast
			this.forecast
					.setDay(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.DAY.toString()
									.toLowerCase())));
			// set month for forecast
			this.forecast
					.setMonth(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MONTH.toString()
									.toLowerCase())));
			// set year for forecast
			this.forecast
					.setYear(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.YEAR.toString()
									.toLowerCase())));
			// set hour
			this.forecast
					.setHour(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.HOUR.toString()
									.toLowerCase())));
			// set time of the day
			this.forecast
					.setTod(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.TOD.toString()
									.toLowerCase())));
			// set predict
			this.forecast.setPredict(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.PREDICT.toString()
							.toLowerCase())));
			// set day of the week
			this.forecast.setWeekday(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.WEEKDAY.toString()
							.toLowerCase())));
		}

		if (ForecastXMLElement.PHENOMENA.toString().equalsIgnoreCase(qName)) {
			// set cloudiness
			this.forecast.setCloudiness(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.CLOUDINESS.toString()
							.toLowerCase())));
			// set precipitation
			this.forecast.setPrecipitation(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.PRECIPITATION.toString()
							.toLowerCase())));
			// set thunderstorm possibility
			this.forecast.setSpower(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.SPOWER.toString()
							.toLowerCase())));
		}

		if (ForecastXMLElement.PRESSURE.toString().equalsIgnoreCase(qName)) {
			// set max pressure
			this.forecast
					.setMaxPressure(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MAX.toString()
									.toLowerCase())));
			// set min pressure
			this.forecast
					.setMinPressure(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MIN.toString()
									.toLowerCase())));
		}

		if (ForecastXMLElement.TEMPERATURE.toString().equalsIgnoreCase(qName)) {
			// set max temperature
			this.forecast
					.setMaxTemperature(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MAX.toString()
									.toLowerCase())));
			// set min temperature
			this.forecast
					.setMinTemperature(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MIN.toString()
									.toLowerCase())));
		}

		if (ForecastXMLElement.WIND.toString().equalsIgnoreCase(qName)) {
			// set max wind velocity
			this.forecast
					.setWindMaxVelocity(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MAX.toString()
									.toLowerCase())));
			// set min wind velocity
			this.forecast
					.setWindMinVelocity(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MIN.toString()
									.toLowerCase())));
			// set wind direction
			this.forecast.setWindDirection(Integer.parseInt(attributes
					.getValue(ForecastXMLElement.DIRECTION.toString()
							.toLowerCase())));
		}

		if (ForecastXMLElement.RELWET.toString().equalsIgnoreCase(qName)) {
			// set max humidity value
			this.forecast
					.setMaxHumidity(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MAX.toString()
									.toLowerCase())));
			// set min humidity value
			this.forecast
					.setMinHumidity(Integer.parseInt(attributes
							.getValue(ForecastXMLElement.MIN.toString()
									.toLowerCase())));
		}

	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (ForecastXMLElement.FORECAST.toString().equalsIgnoreCase(qName)) {
			this.forecastList.add(this.forecast);
			this.town = new Town();
			this.forecast = new Forecast();
		}
	}

}
